package stattracker;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
import java.awt.Image;
import java.awt.Container;
/*
 * --------------GOALS-------------------
 * Should be fine for now.  Might have to leave images as images and not JLabels (see Gamertag Goals).
 * Also might have to integrate with new data structure
 */
public class Halo3ImageFetcher
{
	public Halo3ImageFetcher(Halo3User gt)
	{
		fetchImages(gt);
	}
	private void fetchImages(Halo3User g)
	{
		try
		{
			String tempName = g.getGamertag().replaceAll(" ", "%20");
			String mainURL = "http://www.bungie.net/Stats/Halo3/Default.aspx?player=" + tempName;
			URL bungie = new URL(mainURL);
			BufferedReader in = new BufferedReader(new InputStreamReader(bungie.openStream()));
			String inline = in.readLine();
			while((inline = in.readLine()) != null)
			{
				if(inline.contains("ctl00_mainContent_imgModel")) //Character Model
				{
					String modelURL = getModelURL(inline, true);
					g.setModel(testLabel(modelURL));
					return; // This is the last thing that will be found
				}
				if(inline.contains("ctl00_mainContent_identityStrip_imgRank")) //Rank badge
				{
					String badgeURL = getBadge(inline);
					g.setBadge(getImage(badgeURL));
				}
				if(inline.contains("ctl00_mainContent_identityStrip_divEmblem")) //emblem
				{
					String emblemURL = getModelURL(inline, true);
					g.setEmblem(getImage(emblemURL));
				}
				if(inline.contains("ctl00_mainContent_identityStrip_lblRank")) //current rank
				{
					g.setRank(getRank(inline));
				}
				if(inline.contains("lblSkill")) //highest skill + Total EXP
				{
					g.setSkill(getSkill(inline));
					g.setEXP(getEXP(inline));
				}
				if(inline.contains("<h3>"+g.getGamertag())) //service tag
				{
					g.setServiceTag(getSTag(inline));
				}
			}
		}
		catch(MalformedURLException e){new ErrorMessage(e);}
		catch(IOException e){new ErrorMessage(e);}
	}
	private JLabel getImage(String url) throws MalformedURLException
	{
		return new JLabel(new ImageIcon(new URL(url)));
	}
	private JLabel testLabel(String url) throws MalformedURLException, IOException
	{
		URL imgLoc = new URL(url);
		ImageIcon test = new ImageIcon(imgLoc);
		Container c = new Container();
		Image example = c.createImage((java.awt.image.ImageProducer)imgLoc.getContent());
		test = new ImageIcon(example.getScaledInstance((int)(test.getIconHeight()/1.5), (int)(test.getIconWidth()/1.5), Image.SCALE_AREA_AVERAGING));
		return new JLabel(test);
	}
	private String getSTag(String htmlLine)
	{
		int start = htmlLine.indexOf("-");
		return htmlLine.substring(start+1, htmlLine.indexOf("<", start)).trim();
	}
	private int getSkill(String htmlLine)
	{
		int start = htmlLine.indexOf("lblSkill\">");
		return Integer.parseInt(htmlLine.substring(start+10, htmlLine.indexOf("<", start)));
	}
	private int getEXP(String htmlLine)
	{
		int start = htmlLine.indexOf("TotalRP\">");
		return Integer.parseInt(htmlLine.substring(start+9, htmlLine.indexOf("<", start)));
	}
	private String getRank(String htmlLine)
	{
		int start = htmlLine.indexOf("Global Rank: ");
		return htmlLine.substring(start+13, htmlLine.indexOf("<", start));
	}
	private String getBadge(String htmlLine)
	{
		int src = htmlLine.indexOf("src");
		return "http://www.bungie.net" + htmlLine.substring(src+5, htmlLine.indexOf("\"", src+6));
	}
	private String getModelURL(String htmlLine, boolean replaceAmp)
	{
		int src = htmlLine.indexOf("src");
		String result = htmlLine.substring(src+5, htmlLine.indexOf("\"", src+6));
		if(replaceAmp)
			result = result.replaceAll("amp;", "");
		return "http://www.bungie.net" + result;
	}
}

